﻿<?php
	error_reporting(E_ALL^E_NOTICE);
	require_once ('./config/config.php'); 
	require_once ('./class/email.class.php'); 
	require_once ('./class/tools.class.php');
	date_default_timezone_set("PRC");
	session_start();
	if(empty($_SESSION['user'])){
		echo "<script>location='./login.php';</script>";
		exit();
	}else{
		$userRs=mysqli_fetch_array(mysqli_query($con,"select * from user where user='".$_SESSION['user']."'"));
		if($_GET['oper']=="logout"){
			unset($_SESSION['user']);
			echo "<script>location='./index.php';</script>";
			exit();
		}
		if($_GET['bid']){
			$bookRs=mysqli_fetch_array(mysqli_query($con,"select * from book_sharing where bid='".$_GET['bid']."'"));
			if($_POST['submit']){
				$date=date('Y/m/d H:i:s');
				$day=$_POST['day'];
				$outTradeNo = "booksharingWeb".date('Ymd').substr(implode(NULL, array_map('ord', str_split(substr(uniqid(), 7, 13), 1))), 0, 8);
				mysqli_query($con,"INSERT INTO `order_record` (`rid`, `order_no`, `order_content`, `order_price`, `order_state`, `uid`, `bid`) VALUES (NULL, '".$outTradeNo."', '图书借阅押金', '".$bookRs['book_price']."', '0', '".$userRs['uid']."', '".$_GET['bid']."')");
				$order=mysqli_fetch_array(mysqli_query($con,"SELECT * FROM `order_record` where `order_no`='".$outTradeNo."'"));
				mysqli_query($con,"INSERT INTO `borrowing_record` (`brid`, `uid`, `bid`, `orid`, `submit_time`, `inspect_time`, `days`, `renew`, `new_over_time`, `state`) VALUES (NULL, '".$userRs['uid']."', '".$_GET['bid']."', '".$order['rid']."', '".$date."', '', '".$day."', '0', '', '0')");
				mysqli_query($con,"UPDATE `book_sharing` SET `state` = '1' WHERE `bid` = '".$_GET['bid']."'");
				
				echo "<script>alert('申请已提交！');location='./borrow.php';</script>";
			}
		}else{
			echo "<script>location='./index.php';</script>";
			exit();
		}
	}
?>
<!DOCTYPE html>
<!--[if lt IE 7]>      <html class="no-js lt-ie9 lt-ie8 lt-ie7"> <![endif]-->
<!--[if IE 7]>         <html class="no-js lt-ie9 lt-ie8"> <![endif]-->
<!--[if IE 8]>         <html class="no-js lt-ie9"> <![endif]-->
<!--[if gt IE 8]><!-->
<html class="no-js">
<!--<![endif]-->
	<head>
		<meta charset="utf-8">
		<meta http-equiv="X-UA-Compatible" content="IE=edge">
		<title>某高校图书共享网站</title>
		<meta name="description" content="A responsive web app template with common components like profile, list, tabs, menu views. Made for Codrops exclusively.">
		<meta name="viewport" content="width=device-width, initial-scale=1">
		<link rel="stylesheet" href="./assets/output.css">
		<link rel="stylesheet" href="./assets/node_modules/tippy.js/dist/themes/light.css">
		<link rel="stylesheet" href="./layui/css/layui.css">
		<script src="./layui/js/layui.js" v="layui" e="layui"></script>
	</head>
	<body class="bg-grey-lighter font-sans antialiased">
		<!--[if lt IE 7]>
			<p class="browsehappy">You are using an <strong>outdated</strong> browser. Please <a href="#">upgrade your browser</a> to improve your experience.</p>
		<![endif]-->
		<div class="bg-indigo-darker text-center p-4 px-6 flex items-center">
			<div class="hidden lg:block lg:w-1/4 xl:w-1/5 pr-8">
				<font color="#ffffff"><h1>某高校图书共享网站</h1></font>
			</div>
		</div>
		<!-- Main -->
		<div class="flex">
			<!-- Content -->
			<div class="flex flex-1 flex-col md:px-6 pt-10" id="content">
				<!-- Title -->
				<div class="px-6 md:px-0 flex justify-between items-center -order-1">
					<div>
						<h2 class="font-normal"><a href="./index.php">首页</a> / 图书详情</h2>
					</div>
					<p>欢迎您，<?php echo $_SESSION['user'];?> ! | <a href="./my.php">个人中心</a> | <a href="?oper=logout">注销</a></p>
				</div>
				<!-- Desktop Stats -->
				<div class="hidden px-6 md:px-0 mt-4 md:flex flex-wrap order-1 md:-order-1 md:shadow-md js-tab-pane" id="section-stats">
					<div class="p-4 px-6 w-full md:w-1/2 rounded md:rounded-r-none bg-white shadow-md md:shadow-none">
						<table class="mt-4 w-full">
							<p class="p-2 text-sm leading-loose border-b text-indigo">
								<?php
									$classRs=mysqli_fetch_array(mysqli_query($con,"select * from book_class where cid='".$bookRs['book_class_id']."'"));
									$bookUserRs=mysqli_fetch_array(mysqli_query($con,"select * from user where uid='".$bookRs['uid']."'"));
								?>
							<br />
								图书分类：<?php echo $classRs['class_name'];?>
							<br />
								图书名称：<?php echo $bookRs['book_name'];?>
							<br />
								图书押金：<?php echo $bookRs['book_price'];?> HY币
							<br />
								所属用户：<?php echo $bookUserRs['user'];?>
							<br />
								用户邮箱：<?php echo $bookUserRs['email'];?>
							<br />
								图书封面：<br /><?php echo $bookRs['book_cover'];?>
							<br />
								图书背面：<br /><?php echo $bookRs['book_back'];?>
							</p>
						</table>
					</div>
					<div class="w-full md:w-1/2 p-4 md:pt-12 md:pl-1 mt-4 md:mt-0 lg:pt-12 lg:pl-8 rounded md:rounded-r-none bg-white shadow-md md:shadow-none">
						<div class="flex">
							<div class="w-1/2">
								<p class="text-indigo text-sm p-2">借阅服务</p>
								<?php
									if($bookRs['inspect']=="0"){
										exit("<p class='text-indigo text-sm p-2'>该图书未经审核,暂时无法借阅！</p>");
									}else{
										if($bookRs['state']=="0"){?>
											<form action="./more.php?bid=<?php echo $_GET['bid'];?>" method="post">
												<p class="font-medium text-sm pl-2">
													借阅时常：<select class="layui-input" name="day">
														<option value="7">7天</option>
														<option value="14">14天</option>
														<option value="21">21天</option>
														<option value="30">30天</option>
														<option value="60">60天</option>
														<option value="90">90天</option>
													</select>
													<br />系统当前仅可选择以上借阅时长，借阅期满后仅可申请一次最长30天的续借！
												</p>
												<br />
												<p class="text-indigo text-sm p-2">
													<input type="submit" name="submit" class="layui-btn" value="申请借阅">
												</p>
											</form>
								<?php
										}else{
											exit("<p class='text-indigo text-sm p-2'>该图书已被其他人借走了,请再转转吧！</p>");
										}
									}
								?>
							</div>
						</div>
					</div>
				</div>
			</div>
		</div>
		<script src="./assets/bundle.js" async defer></script>
</body>
</html>